package com.sskj.miner.ui.activity;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.TipUtil;
import com.sskj.miner.R;
import com.sskj.miner.R2;
import com.sskj.miner.bean.BibPaoBean;
import com.sskj.miner.bean.IWaterBean;
import com.sskj.miner.bean.MinerAssetBean;
import com.sskj.miner.bean.PaoBean;
import com.sskj.miner.bean.UsdtPaoBean;
import com.sskj.miner.presenter.MiningActivityPresenter;
import com.sskj.miner.widget.WaterView;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.Flowable;


@Route(path = RConfig.MINER_MAIN) // 挖矿
public class MiningActivity extends BaseActivity<MiningActivityPresenter> {

    @BindView(R2.id.waterView)
    WaterView waterView;
    @BindView(R2.id.trade)
    TextView tradeTv;
    @BindView(R2.id.mining_yl)
    LinearLayout miningYl;
    @BindView(R2.id.ll_gct)
    LinearLayout llGct;
    @BindView(R2.id.ll_usdt)
    LinearLayout llUsdt;
    @BindView(R2.id.llRule)
    LinearLayout llRule;
    @BindView(R2.id.mining_yl_num)
    TextView miningYlNum;
    @BindView(R2.id.rl_gct_num)
    TextView rlGctNum;
    @BindView(R2.id.rl_usdt_num)
    TextView rlUsdtNum;

    private MaterialDialog taskTip;
    private MaterialDialog ruleTip;

    @Override
    protected int getLayoutId() {
        return R.layout.miner_activity_mining;
    }

    @Override
    public MiningActivityPresenter getPresenter() {
        return new MiningActivityPresenter();
    }

    @Override
    protected void initView() {
        RxBus.getDefault().register(this);

        ClickUtil.click(tradeTv, () -> {
            RxBus.getDefault().send(RxBusCode.CHANGE_THIRD);
            finish();
        });
        ClickUtil.click(miningYl, () -> {
            ARouter.getInstance().build(RConfig.MINER_FORCE).navigation();

        });

        ClickUtil.click(llGct, () -> {
            ARouter.getInstance().build(RConfig.MINER_PROFILE).withString(Constans.CODE, "DIG").navigation();

        });
        ClickUtil.click(llUsdt, () -> {
            ARouter.getInstance().build(RConfig.MINER_PROFILE).withString(Constans.CODE, "USDT").navigation();
        });
        ClickUtil.click(llRule, () -> {
            mPresenter.getRule();
        });
    }

    @Override
    protected void initData() {
        mPresenter.getAsset();
        mPresenter.getPao();
    }

    @Override
    public boolean getImmersion() {
        return false;
    }


    public void updateUi(MinerAssetBean miningQPBean) {

        rlUsdtNum.setText(miningQPBean.getTotalUsdt());
        rlGctNum.setText(miningQPBean.getTotalBib());
        miningYlNum.setText(miningQPBean.getMineForce());
    }


    public void removeWaterView(View view) {
        waterView.removeWater(view);
    }


    public void showNotice(String content) {
        ruleTip = TipUtil.getSureTip(this, App.INSTANCE.getString(R.string.miner_miningActivity1), content, App.INSTANCE.getString(R.string.miner_miningActivity2), () -> {
            ruleTip.dismiss();
        });
    }

    public void updatePao(PaoBean data) {

        if (data == null) {
            return;
        }
        ArrayList<IWaterBean> iWaterBeans = new ArrayList<>();
        for (int i = 0; i < data.getUSDT().getNum(); i++) {
            iWaterBeans.add(new UsdtPaoBean(data.getUSDT().getCode(), data.getUSDT().getEveryValue(), R.mipmap.miner_icon_usdt_small));
        }
        for (int i = 0; i < data.getBIB().getNum(); i++) {
            iWaterBeans.add(new BibPaoBean(data.getBIB().getCode(), data.getBIB().getEveryValue(), R.mipmap.miner_icon_bib));
        }
        Flowable.fromIterable(data.getLargePao())
                .filter(largePaoBean -> largePaoBean.getTaskComplete() != 3)
                .toList()
                .subscribe((largePaoBeans, throwable) -> {
                    iWaterBeans.addAll(largePaoBeans);
                    waterView.setWaters(iWaterBeans);
                    waterView.setOnWaterViewClick((waterData, view) -> {
                        Object tag = view.getTag();
                        if (tag instanceof UsdtPaoBean) {
                            UsdtPaoBean usdtPaoBean = (UsdtPaoBean) tag;
                            mPresenter.receivePao(usdtPaoBean.getCode(), usdtPaoBean.getNum(), "2", null, view);
                        } else if (tag instanceof BibPaoBean) {
                            BibPaoBean usdtPaoBean = (BibPaoBean) tag;

                            mPresenter.receivePao(usdtPaoBean.getCode(), usdtPaoBean.getNum(), "2", null, view);

                        } else if (tag instanceof PaoBean.LargePaoBean) {

                            PaoBean.LargePaoBean largePaoBean = (PaoBean.LargePaoBean) tag;
                            if (largePaoBean.getTaskComplete() == 1) {
                                showFail(largePaoBean);
                                return;
                            }
                            mPresenter.receivePao("USDT", largePaoBean.getValue(), "1", largePaoBean.getType(), view);

                        }
                    });
                });

    }

    private void showFail(PaoBean.LargePaoBean largePaoBean) {
        taskTip = TipUtil.getSureTip(this, App.INSTANCE.getString(R.string.miner_miningActivity3), largePaoBean.getTaskIntro(), App.INSTANCE.getString(R.string.miner_miningActivity4), () -> {

            taskTip.dismiss();
            switch (largePaoBean.getType()) {
                case "1":
                    RxBus.getDefault().send(RxBusCode.CHANGE_THIRD);
                    finish();
                    break;
                case "2":
                    ARouter.getInstance().build(RConfig.MINE_INVITE_URL).navigation();
                    finish();
                    break;
                case "3":
                case "4":
                case "5":
                    ARouter.getInstance().build(RConfig.MINE_RECHARGE).navigation();
                    finish();
                    break;
            }
        });
    }
}
